package component;

import Prism.core.AbstractImplementation;
import Prism.core.Event;
import Prism.core.PrismConstants;
import basicTypes.UserRecord;

public class CustomerSupport extends AbstractImplementation {
	// prov Name(Event Name)
	public static final String provCreateNewUser = "createNewUser";
	public static final String provProvideUserData = "provideUserData";
	public static final String provSendUserDataOperation = "sendUserDataOperation";
	public static final String provProvidePhoneNumber = "providePhoneNumber";
	public static final String provShowTime = "showTime";
	
	//newUser: UserRecordType;
	UserRecord newUser;
	
	//currentUser: UserRecordType;
	UserRecord currentUser;
	
	//freePhoneNumber: PhoneNumber;
	String freePhoneNumber;
	
	@Override
	public void start() {
		super.start();
		newUser = new UserRecord("0","NOBODY","0","000000000");
		currentUser = new UserRecord("0","NOCURRENT","0","000000000");
		freePhoneNumber = "000";
	}
	
	@Override
	public void handle(Event e) {
		// Handle of request of provCreateNewUser
		if(e.getName().equals(provCreateNewUser)) {
			// send request to upper layer
			Prism.core.Event r = new Prism.core.Event(provCreateNewUser, PrismConstants.REPLY);
			r.addParameter("time", newUser.getTime());
			r.addParameter("name", newUser.getName());
			r.addParameter("amount", newUser.getAmount());
			r.addParameter("phoneNumber", newUser.getPhoneNumber());
			this.send(r);
		}
		// Handle of request of provProvideUserData
		else if(e.getName().equals(provProvideUserData)) {
			// send request to upper layer
			Prism.core.Event r = new Prism.core.Event(provProvideUserData, PrismConstants.REPLY);
			r.addParameter("time", currentUser.getTime());
			r.addParameter("name", currentUser.getName());
			r.addParameter("amount", currentUser.getAmount());
			r.addParameter("phoneNumber", currentUser.getPhoneNumber());
			this.send(r);
		}
		// Handle of request of provSendUserDataOperation
		else if(e.getName().equals(provSendUserDataOperation)) {
			UserRecord tempRecord = new UserRecord(
					(String) e.getParameter("time"),
					(String) e.getParameter("name"),
					(String) e.getParameter("amount"),
					(String) e.getParameter("phoneNumber")
					);
			
			submitUserDataAndOperation(tempRecord,(String) e.getParameter("operation"));
		}
		// Handle of request of provProvidePhoneNumber
		else if(e.getName().equals(provProvidePhoneNumber)) {
			obtainAvailablePhoneNumber((String) e.getParameter("taskID"));
		}
		// Handle of request of provShowTime
		else if(e.getName().equals(provShowTime)) {
			getTime((String) e.getParameter("taskID"));
		}
		/////////////////////////////////////////////////////////////////
		// Handle of request reply of TransConsistCheck.provShowUserData
		else if(e.getName().equals(TransConsistCheck.provShowUserData)) {
			UserRecord tempUser = new UserRecord(
					(String) e.getParameter("time"),
					(String) e.getParameter("name"),
					(String) e.getParameter("amount"),
					(String) e.getParameter("phoneNumber")
					);
			
			// send request to upper layer
			Prism.core.Event r = new Prism.core.Event(provProvideUserData, PrismConstants.REPLY);
			r.addParameter("time", tempUser.getTime());
			r.addParameter("name", tempUser.getName());
			r.addParameter("amount", tempUser.getAmount());
			r.addParameter("phoneNumber", tempUser.getPhoneNumber());
			this.send(r);
			
		}
		// Handle of request reply of TransConsistCheck.provObtainAvailablePhoneNumber
		else if(e.getName().equals(TransConsistCheck.provObtainAvailablePhoneNumber)) {
			freePhoneNumber = (String) e.getParameter("phoneNumber");
			Prism.core.Event r = new Prism.core.Event(provProvidePhoneNumber, PrismConstants.REPLY);
			r.addParameter("phoneNumber", freePhoneNumber);
			this.send(r);
		}
		// Handle of request reply of TransConsistCheck.provGetTime
		else if(e.getName().equals(TransConsistCheck.provGetTime)) {
			Prism.core.Event r = new Prism.core.Event(provShowTime, PrismConstants.REPLY);
			r.addParameter("time", (String) e.getParameter("time"));
			this.send(r);
		}
		
	}
	
	public void submitUserDataAndOperation(UserRecord ur, String op) {
		// send request to upper layer
		Prism.core.Event r = new Prism.core.Event(TransConsistCheck.provSubmitUserDataAndOperation, PrismConstants.REQUEST);
		r.addParameter("time", ur.getTime());
		r.addParameter("name", ur.getName());
		r.addParameter("amount", ur.getAmount());
		r.addParameter("phoneNumber", ur.getPhoneNumber());
		r.addParameter("operation", op);
		this.send(r);
	}
	
	public void obtainAvailablePhoneNumber(String tid) {
		// send request to upper layer
		Prism.core.Event r = new Prism.core.Event(TransConsistCheck.provObtainAvailablePhoneNumber, PrismConstants.REQUEST);
		r.addParameter("taskID", tid);
		this.send(r);
	}
	
	public void getTime(String tid) {
		// send request to upper layer
		Prism.core.Event r = new Prism.core.Event(TransConsistCheck.provGetTime, PrismConstants.REQUEST);
		r.addParameter("taskID", tid);
		this.send(r);
	}
}
